package com.example.criminalintent


import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.example.criminalintent.model.Crime
import com.example.criminalintent.view.CrimeFragment
import com.example.criminalintent.view.CrimeListFragment

class CrimeListActivity : SingleFragmentActivity(), CrimeListFragment.Callbacks,
    CrimeFragment.Callbacks {
    override fun onCrimeUpdate(crime: Crime) {
        val listFragment =
            supportFragmentManager.findFragmentById(R.id.fragment_container) as CrimeListFragment
        listFragment.updateUI()
    }

    override fun onCrimeSelected(crime: Crime) {
        if (findViewById<FrameLayout>(R.id.detail_fragment_container) == null) {
            val intent = CrimePagerActivity.newIntent(this, crime.mId!!)
            startActivity(intent)
        } else {
            val instance = CrimeFragment.newInstance(crime.mId!!)
            supportFragmentManager.beginTransaction()
                .replace(R.id.detail_fragment_container, instance)
                .commit()
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_masterdetail
    }

    override fun createFragment(): Fragment {
        return CrimeListFragment()
    }
}
